package top.buaarobot.webserver.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.buaarobot.webserver.domain.MarkdownFile;
import top.buaarobot.webserver.domain.User;
import top.buaarobot.webserver.repository.MarkdownFileRepository;
import top.buaarobot.webserver.repository.UserRepository;

import java.util.*;

@Service
@Transactional
public class UserService {

    private final UserRepository userRepository;
    private final MarkdownFileRepository markdownFileRepository;

    public UserService(UserRepository userRepository, MarkdownFileRepository markdownFileRepository) {
        this.userRepository = userRepository;
        this.markdownFileRepository = markdownFileRepository;
    }

    public JSONArray getActiveUsers(Integer count){
        JSONArray result = new JSONArray();
        Iterable<MarkdownFile> markdownFiles =  markdownFileRepository.findAll();
        HashMap<String,Integer> name2Count = new HashMap<>();
        for(MarkdownFile mdfile: markdownFiles){
            if(name2Count.containsKey(mdfile.getAuthor())){
                name2Count.put(mdfile.getAuthor(),name2Count.get(mdfile.getAuthor()) + 1);
            }
            else{
                name2Count.put(mdfile.getAuthor(), 1);
            }
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(name2Count.entrySet()); //转换为list
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        if(count == -1 || count > list.size()){
            for(int i = 0; i < list.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                List<User> user = userRepository.findByUsername(list.get(i).getKey());
                if(user.size() > 0){
                    jsonObject.put("username", user.get(0).getUsername());
                    jsonObject.put("nickname", user.get(0).getNickname());
                }
                else{
                    jsonObject.put("username", "unknown");
                    jsonObject.put("nickname", "unknown");
                }
                result.add(jsonObject);
            }
        }
        else{
            for(int i = 0; i < count; i++){
                JSONObject jsonObject = new JSONObject();
                List<User> user = userRepository.findByUsername(list.get(i).getKey());
                if(user.size() > 0){
                    jsonObject.put("username", user.get(0).getUsername());
                    jsonObject.put("nickname", user.get(0).getNickname());
                }
                else{
                    jsonObject.put("username", "unknown");
                    jsonObject.put("nickname", "unknown");
                }
                result.add(jsonObject);
            }
        }
        return result;
    }
}
